<?php
class AdminLib {
	private $model;
	function __construct() {
		require_once APP_MODEL_PATH . 'e_administrate.php';
		$this->model = new AdminModel ();
	}
	public function getByAdminName($adminName) {
		return $this->model->getByAdminName ( $adminName );
	}
	public function getById($id) {
		return $this->model->getById ( $id );
	}
	public function getList() {
		return $this->model->getList ();
	}
	public function delAdminById($id) {
		return $this->model->delById ( $id );
	}
	public function setInfoById($info, $id) {
		return $this->model->setInfoById ( $info, $id );
	}
	public function addInfo($info) {
		$data->AdminName = $info->AdminName;
		$data->Password = md5 ( $info->Password );
		$data->TrueName = $info->TrueName;
		$data->LastLoginTime = AdminVisitor::getDateTime ();
		$data->LastLoginIp = AdminVisitor::getIp ();
		$data->AdminGrant = $info->AdminGrant;
		return $this->model->addInfo ( $data );
	}
	public function setPasswordByAdminName($password, $adminName) {
		$data->Password = $password;
		return $this->model->setInfoByAdminName ( $data, $adminName );
	}
	public function editInfoByAdminName($info) {
		if ($info->Password) {
			$data->Password = md5 ( $info->Password );
		}
		$data->TrueName = $info->TrueName;
		$data->AdminGrant = $info->AdminGrant;
		return $this->model->setInfoByAdminName ( $data, $info->AdminName );
	}
	public function checkGrand($key, $isNeedGradeId) {
		if (! AdminSession::getAdminSessionInfo ()) {
			throw new Exception ( "对不起请先登录系统" );
		}
		if ($key == 'camp' || $key == 'fullTime' || $key == 'lotus') {
			if ($isNeedGradeId) {
				if (! isset ( $_GET ['gradeId'] )) {
					
					throw new Exception ( "对不起您没有权限访问" );
				}
				$gradeId = $_GET ['gradeId'];
				return $this->checkGradeId ( $key, $gradeId );
			} else {
				return TRUE;
			}
		}
		switch ($key) {
			case 'finance' :
				$this->checkey ( $key );
				break;
			case 'manage' :
				$this->checkey ( $key );
				break;
			case 'person' :
				$this->checkey ( $key );
			case 'teacher' :
				$this->checkey ( $key );
				break;
			default :
				throw new Exception ( "对不起您没有权限访问" );
				break;
		}
	}
	public function checkGradeId($key, $gradeId) {
		$adminGrant=AdminVisitor::getAdminGrant();
		if (isset ( $adminGrant [$key] )) {
			if (in_array ( $gradeId, $adminGrant [$key] )) {
				return TRUE;
			}
		}
		throw new Exception ( "对不起您没有权限访问" );
	}
	private function checkey($key) {
		$userGrand=AdminVisitor::getAdminGrant();
		if (! isset ( $userGrand[$key] )) {
			throw new Exception ( "对不起您没有权限访问" );
		}
		return TRUE;
	}
}